package com.mg.controller;

import com.github.pagehelper.PageInfo;
import com.mg.common.Constants;
import com.mg.dto.RechargeRecordDto;
import com.mg.dto.SimpleResult;
import com.mg.model.RechargeRecord;
import com.mg.model.User;
import com.mg.service.RechargeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by mg1213 on 2018/3/18.
 */
@Controller
@RequestMapping("/rechargeRecord")
public class RechargeRecordController {
    @Autowired
    private RechargeRecordService rechargeRecordService;

    @RequestMapping(value = "/user/selectPageInfo")
    @ResponseBody
    public PageInfo<RechargeRecordDto> selectUserPageInfo(RechargeRecordDto dto, int limit, int start) {
        return rechargeRecordService.selectPageInfo(dto, start, limit);
    }

    @RequestMapping(value = "/admin/selectPageInfo")
    @ResponseBody
    public PageInfo<RechargeRecordDto> selectPageInfo(RechargeRecordDto dto, int limit, int start) {
        return rechargeRecordService.selectPageInfo(dto, start, limit);
    }

    @RequestMapping(value = "/admin/insertRechargeRecord")
    @ResponseBody
    public SimpleResult insertRechargeRecord(RechargeRecord record) {
        return rechargeRecordService.insertRechargeRecord(record);
    }

    @RequestMapping(value = "/user/saveRechargeRecord")
    @ResponseBody
    public SimpleResult saveRechargeRecord(RechargeRecord record, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(Constants.USER_INFO);
        record.setUserId(user.getId());
        if (record.getId() == null) {
            return rechargeRecordService.insertRechargeRecord(record);
        } else {
            return rechargeRecordService.updateRechargeRecord(record);
        }

    }

    @RequestMapping(value = "/admin/updateRechargeRecord")
    @ResponseBody
    public SimpleResult updateRechargeRecord(RechargeRecord record) {
        SimpleResult result;
        if (record.getId() != null) {
            result = rechargeRecordService.updateRechargeRecord(record);
        } else {
            result = rechargeRecordService.insertRechargeRecord(record);
        }
        return result;
    }

    @RequestMapping(value = "/admin/deleteRechargeRecord")
    @ResponseBody
    public SimpleResult deleteRechargeRecord(String ids) {
        return rechargeRecordService.deleteRechargeRecord(ids);
    }

    @RequestMapping(value = "/user/deleteRechargeRecord")
    @ResponseBody
    public SimpleResult deleteRechargeRecord(Integer id) {

        return rechargeRecordService.deleteRechargeRecord(id + "");
    }


}
